package de.tempel.fittslaw.regression;

import java.awt.geom.Point2D;
import java.util.List;

public class EasyRegressionCalculator implements RegressionCalculator {

	@Override
	public Line calculateRegression(List<Point2D> points) {
		int n = points.size();

		double x[] = new double[n];
		double y[] = new double[n];

		double sumX = 0;
		double sumY = 0;

		int i = 0;
		for (Point2D p : points) {
			x[i] = p.getX();
			y[i] = p.getY();

			sumX += x[i];
			sumY += y[i];

			// System.out.println("(" + x[i] + "," + y[i] + ")");
			i++;

		}

		double meanX = sumX / n;
		double meanY = sumY / n;

		double zaehler = 0;
		double nenner = 0;

		for (int j = 0; j < n; j++) {
			zaehler = zaehler + (x[j] - meanX) * (y[j] - meanY);
			nenner = nenner + (x[j] - meanX) * (x[j] - meanX);
		}

		double b = zaehler / nenner;
		double a = meanY - b * meanX;

		return new Line(a, b);
	}

}
